package gold.digger;

import gold.vo.*;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC199 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public List<Integer> rightSideView(TreeNode root) {
            List<Integer> res = new ArrayList<>();
            Deque<TreeNode> curLayer = new LinkedList<>();
            Deque<TreeNode> nextLayer = new LinkedList<>();
            curLayer.offerLast(root);
            TreeNode cur = root;

            while (!curLayer.isEmpty() && null != cur) {
                while (!curLayer.isEmpty()) {
                    cur = curLayer.pollFirst();
                    if (null != cur.left) nextLayer.offerLast(cur.left);
                    if (null != cur.right) nextLayer.offerLast(cur.right);
                }

                res.add(cur.val);
                Deque<TreeNode> temp = curLayer;
                curLayer = nextLayer;
                nextLayer = temp;
            }

            return res;
        }
    }

    public void run() {
        Solution solution = new Solution();
        TreeNode root = TreeNode.createTree("1,2,3,4,5");
        System.out.println(solution.rightSideView(null));
    }

    public static void main(String[] args) throws Exception {
        LC199 an = new LC199();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
